package com.by.shop.order.controller;

import com.alibaba.fastjson.JSON;
import com.by.shop.common.entity.Order;
import com.by.shop.common.entity.Product;
import com.by.shop.order.service.OrderService;
import com.by.shop.order.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * User: zhudi
 */
@RestController
@Slf4j
@RequestMapping("order2")
public class OrderController2 {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ProductService productService;

    @GetMapping("prod/{pid}")
    public Order order(@PathVariable Integer pid) {
        log.info("客户收到{}号商品的下单请求，接下来调用商品服务查找次商品信息", pid);
        // 使用openFeign调用 商品服务的接口
        Product product = productService.findById(pid);
        log.info("查询到{}号商品信息，内容是：", pid, JSON.toJSONString(product));

        // 模拟一次网络延迟
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 为了不产生很多垃圾订单，不做订单保存
        if (product != null) {
            Order order = new Order();
            order.setUid(1);
            order.setUsername("测试用户");


            //orderService.save(order);

            log.info("创建订单成功，订单信息为：{}", JSON.toJSONString(order));

            return order;
        }

        return null;
    }

    @RequestMapping("message")
    public String message() {
        return "高并发下的问题测试";
    }
}
